package com.dualsportmaps.android;

import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.provider.MediaStore;

public class Utils {
	public static int brighter(int c) {
		   
        int r = Color.red(c);
        int b = Color.blue(c);
        int g = Color.green(c);

        if(r == 0 && b == 0 && g == 0) {
            return Color.DKGRAY;
        }

        if(r < 3 && r != 0) {
            r = 3;
        } else {
            r = (int) (r/.7);
            r = (r > 255) ? 255 : r;
        }

        if(b < 3 && b != 0) {
            b = 3;
        } else {
            b = (int) (b/.7);
            b = (b > 255) ? 255 : b;
        }

        if(g < 3 && g != 0) {
            g = 3;
        } else {
            g = (int) (g/.7);
            g = (g > 255) ? 255 : g;
        }

        return Color.rgb(r, g, b);
    }
	public static int darker(int c) {
		   
        int r = Color.red(c);
        int b = Color.blue(c);
        int g = Color.green(c);

        return Color.rgb((int)(r*.7), (int)(g*.7), (int)(b*.7));
    }
	public static int pastel(int c) {
		int r = Color.red(c);
        int b = Color.blue(c);
        int g = Color.green(c);
        if (r >= b && r >= g) {
        	b = pastelCalc(b);
        	g = pastelCalc(g);
        } else if (g >= r && g >= b) {
        	b = pastelCalc(b);
        	r = pastelCalc(r);
        } else if (b >= r && b >= g) {
        	r = pastelCalc(r);
        	g = pastelCalc(g);
        }
        return Color.rgb(r,g,b);
	}
	private static int pastelCalc(int c) {
		return (c+200)/2;
	}
	
    public static String getContentName(ContentResolver resolver, Uri uri){
        Cursor cursor = resolver.query(uri, null, null, null, null);
        cursor.moveToFirst();
        int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
        if (nameIndex >= 0) {
            return cursor.getString(nameIndex);
        } else {
            return null;
        }
    }
    
    //output kilometers
    public static double distance(double lat1, double lon1, double lat2, double lon2) {
    	double theta = lon1 - lon2;
    	double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
    	dist = Math.acos(dist);
    	dist = rad2deg(dist);
    	dist = dist * 60 * 1.1515 * 1.609344;
    	return (dist);
    }
   	private static double deg2rad(double deg) {
   		return (deg * Math.PI / 180.0);
    }
   	private static double rad2deg(double rad) {
   		return (rad * 180.0 / Math.PI);
   	}
}
